package com.atguigu.gulimall.search.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.to.es.SkuEsmodel;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @program: gulimall
 * @description: es保存服务
 * @author: liuxvru
 * @create: 2024-03-02 14:58
 **/
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {
    @Autowired
    private ProductSaveService productSaveService;

    @PostMapping("/product")
    //@RequiresPermissions("product:spuinfo:list")
    public R productStatusUp(@RequestBody List<SkuEsmodel> upProducts) {
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(upProducts);
        } catch (Exception e) {
            log.error("ES商品上架异常,{ }", e);
            return R.error(BizCodeEnume.PRODUCT_UP_VAILD_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_VAILD_EXCEPTION.getMsg());
        }
        if (b) {
            log.error("ES商品上架异常");
            return R.error(BizCodeEnume.PRODUCT_UP_VAILD_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_VAILD_EXCEPTION.getMsg());
        }
        return R.ok();
    }
}
